#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2019/6/11 17:36
# @Auther  : Frank
# @Email   : 550862872@qq.com
# @File    : sqlheper.py
# @Software: PyCharm
import pymysql

def get_list(sql,args):
    conn = pymysql.connect(host='192.168.198.133', db='student_management', port=3306, user='root',
                           passwd='123qaz!@#QAZ', charset='utf8')

    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

    cursor.execute(sql,args)

    result = cursor.fetchall()

    cursor.close()

    conn.close()

    return result

def get_one(sql, args):
    '''
        获取一条信息
    :param sql:
    :param args:
    :return:
    '''
    conn = pymysql.connect(host='192.168.198.133', db='student_management', port=3306, user='root',
                           passwd='123qaz!@#QAZ', charset='utf8')
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    cursor.execute(sql, args)
    result = cursor.fetchone()
    cursor.close()
    conn.close()
    return result


def modify(sql, args):
    conn = pymysql.connect(host='192.168.198.133', db='student_management', port=3306, user='root',
                           passwd='123qaz!@#QAZ', charset='utf8')
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    cursor.execute(sql,args)
    conn.commit()
    cursor.close()
    conn.close()

def create(sql,args):
    conn = pymysql.connect(host='192.168.198.133', db='student_management', port=3306, user='root',
                           passwd='123qaz!@#QAZ', charset='utf8')
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    cursor.execute(sql, args)
    conn.commit()
    laet_row_id = cursor.lastrowid
    cursor.close()
    conn.close()
    return laet_row_id




class SqlHeper(object):

    def __init__(self):
        self.connect()

    def connect(self):
        self.conn = pymysql.connect(host='192.168.198.133', db='student_management', port=3306, user='root',
                               passwd='123qaz!@#QAZ', charset='utf8')
        self.cursor = self.conn.cursor(cursor=pymysql.cursors.DictCursor)

    def get_list(self,sql,args):
        self.cursor.execute(sql, args)
        result = self.cursor.fetchall()
        return result
    def get_one(self,sql,args):
        self.cursor.execute(sql, args)
        result = self.cursor.fetchone()
        return result

    def modify(self,sql,args):
        self.cursor.execute(sql, args)
        self.conn.commit()

    def multiple_modify(self,sql,args):
        self.cursor.executemany(sql, args)
        self.conn.commit()

    def create(self, sql, args):
        self.cursor.execute(sql, args)
        self.conn.commit()
        return self.cursor.lastrowid

    def close(self):
        self.cursor.close()
        self.conn.close()